//
//  HTmainViewController.swift
//  mySinaBlog
//
//  Created by whtapple on 3/21/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit

class HTmainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //加载子控件
        addcontrollers()
        //设置 tabbar为自定义的
        let newTabbar = HTtabbar()
        setValue(newTabbar, forKey: "tabBar")
        tabBar.backgroundColor = UIColor.whiteColor()
        newTabbar.composeCallback = {
            //未登录，就不能发微博rootViewController
            if HTUserAccountViewModel.sharedInstance.isLogin() == false {
                return
            }
            //拿到产品名称
            let productName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? String ?? ""
            //拿到类名
            let cls = NSClassFromString("\(productName).HTComposeViewController") as! UIViewController.Type
            //通过类名创建控制器
            let composeVC = cls.init()
             //莫大出控制器
            self.presentViewController(UINavigationController(rootViewController:composeVC), animated: true, completion: nil)
        }
    }
    
    //mark-------添加子控件
    //新建并添加子控制器
    func addcontrollers(){
        
        let homeVC = HTHomeTableViewController()
        addChildViewController(homeVC, Title: "首页", imageName: "tabbar_home")
        let discoverVC = HTDiscoverTableViewController()
        addChildViewController(discoverVC, Title: "发现", imageName: "tabbar_discover");
        let messageVC = HTMessageTableViewController()
        addChildViewController(messageVC, Title: "消息", imageName: "tabbar_message_center");
        let profileVC = HTProfileTableViewController()
        addChildViewController(profileVC, Title: "我", imageName: "tabbar_profile")
    }
    //mark ----------- 设置子控件
    //给子控制器设置属性
    func addChildViewController(childController: UIViewController,Title: String,imageName: String) {
        //设置名称
        childController.title = Title
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片
        let selImageName = imageName + "_highlighted"
        childController.tabBarItem.selectedImage = UIImage(named: selImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected);
        let navcontroller = UINavigationController(rootViewController: childController);
        //添加子控制器
        addChildViewController(navcontroller)
    }
}
